package com.geniusk.system.service.impl;

import com.geniusk.system.model.mapper.RoleMenuMapper;
import com.geniusk.system.model.pojo.Role;
import com.geniusk.system.model.pojo.RoleMenu;
import com.geniusk.system.service.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public int insertRoleMenu(Role role) {
        roleMenuMapper.deleteRoleMenuByRoleId(role.getRoleId());
        Long[] menuIds = role.getMenuIds();
        if (menuIds != null && menuIds.length > 0) {
            // 新增角色菜单关系
            List<RoleMenu> list = new ArrayList<>();
            for (Long menuId : menuIds) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(role.getRoleId());
                roleMenu.setMenuId(menuId);
                list.add(roleMenu);
            }
            return roleMenuMapper.insertRoleMenu(list);
        }
        return 0;
    }
}
